<template>
  <el-container class="el-container-home">
    <!--头部区域-->
    <el-header>
      <div class="head_title">
        <img src="../assets/logo.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <!--主体区域-->
    <el-container>
    <!--左侧-->
      <el-aside :width="isCollapse? '64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
            :default-active="activePath"
            class="el-menu-vertical-demo"
            background-color="#373D41"
            text-color="#fff"
            unique-opened
            active-text-color="#2376b7"
            :collapse="isCollapse"
            :collapse-transition="false"
            :router="true"
            @select="menuActive"
        >
          <!--一级菜单-->
          <el-submenu :index="item.id+''" v-for="(item,index) in menusList" :key="item.id">
            <template slot="title">
              <i :class="iconsList[index]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <!--二级菜单-->
            <el-menu-item :index="child.path" v-for="child in item.children" :key="child.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ child.authName }}</span>
              </template>

            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
    <!--主体-->
      <el-main>
<!--        welcome 占位符-->

        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import {getMenusList} from "@/utils/loginAPI";


export default {
  name: "Home",
  created() {
    this.initMenusList()
  },
  data(){
    return{
      menusList:[],
      iconsList:[
          'el-icon-user-solid',
          'el-icon-bank-card',
          'el-icon-shopping-bag-1',
          'el-icon-s-order',
          'el-icon-s-marketing'
      ],
      isCollapse:false,
      activePath:'',
    }
  },
  methods:{
    loginOut(){
      //清除token
      window.sessionStorage.removeItem('token');
      //跳转到登录页
      this.$router.push('/login');
    },
    //获取导航菜单
    async initMenusList(){
      const {data:menus}=await getMenusList();
      // console.log(menus);
      if(menus.meta.status!==200) return this.$message.error(menus.meta.msg);
      this.menusList=menus.data;
    },
    toggleCollapse(){
      this.isCollapse=!this.isCollapse
    },
    menuActive(index){
      //获取二级菜单地址 users roles
      this.activePath= index;
      // console.log(index);
    }
  }
}
</script>

<style scoped lang="less">
.el-menu{
  border: none;
}
.el-container-home{
  color: white;
  height: 100%;
  .el-header{
    background-color: #373D41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .head_title{
      font-size: 20px;
      img{
        height: 60px;
        vertical-align: middle;
      }
    }

  }
}

    .el-aside {
      background-color: #373D41;
      transition: 0.5s;
      .toggle-button{
        background-color: #171616;
        text-align: center;
        letter-spacing: 0.2em;
        font-size: 15px;
        height: 24px;
        line-height: 24px;
        cursor: pointer;
      }

    }
    .el-main {
      color: #171616;
      background-color:#e9edf1 ;
    }
</style>
